package com.park.controller;

import com.park.common.constant.IPlatformConstant;
import com.park.common.poo.UserOperator;
import com.park.common.util.StrUtil;
import com.park.platform.service.IOperatorService;
import com.park.wx.WeiXinConnector;
import com.park.wx.pojo.SNSUserInfo;
import com.park.wx.pojo.WeixinOauth2Token;
import com.park.wx.util.AdvancedUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**授权后的回调请求处理
 * 
 * @author Administrator
 *
 */
@Controller
public class OAuth2Controller extends BaseController {

	private static final long serialVersionUID = -1847238807216447030L;

	private static Logger logger = Logger.getLogger(OAuth2Controller.class);

	@Autowired
	private IOperatorService operatorService;

	@RequestMapping(value = "oAuth2", method = RequestMethod.GET)
	public void oAuth2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String openId = StrUtil.EMPTY;
		try{
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			// 用户同意授权后，能获取到code
			String code = request.getParameter("code");
			String state = request.getParameter("state");//URLDecoder.decode(SymmetricEncoder.decode(request.getParameter("state")), "UTF-8");
			//state = URLDecoder.decode(state, "UTF-8");
			logger.info("state:" + state);

			String returnUrl = state;
			String shareOpenId = null;
			if(state.contains(IPlatformConstant.SHARE_ID)) { //分享
				///package/index?aaa=1&bbb=2&shareId=a123456789
				returnUrl = state.substring(0, state.lastIndexOf("&")); //截取掉分享人的openId
				shareOpenId = state.substring(state.lastIndexOf("=") + 1); //截取出分享人openId
			}
			// 用户同意授权
			if (!"authdeny".equals(code)) {
				// 获取网页授权access_token,应用的appid和appsecret
				WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken(WeiXinConnector.APP_ID, WeiXinConnector.SECRET, code);
				// 网页授权接口访问凭证
				String accessToken = weixinOauth2Token.getAccessToken();
				// 用户标识
				openId = weixinOauth2Token.getOpenId();
				// 获取用户信息
				SNSUserInfo snsUserInfo = AdvancedUtil.getSNSUserInfo(accessToken, openId);

				// 设置要传递的参数
				request.setAttribute("snsUserInfo", snsUserInfo);

				UserOperator operator = operatorService.saveShareOperator(shareOpenId, snsUserInfo);

				if(operator == null) throw new Exception();

				logger.info("----------------------------------------------------------：" + openId);

				request.getSession().setAttribute(IPlatformConstant.LOGIN_USER_MOBILE, operator);
			}
			response.sendRedirect(returnUrl);
			//return redirect(returnUrl);
		}catch (Exception e){
			e.printStackTrace();
		}
		// 跳转到显示信息页
		//return "user/user";
	}



}
